package com.longrise.android.byjk.permission;

import android.app.ActionBar;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.TextView;

import com.longrise.android.byjk.R;
import com.longrise.common.utils.AppUtil;


/**
 * Created by callxkj on 2016/12/21.
 * 提示权限的dialog
 */

public class PermissionDialog extends Dialog implements View.OnClickListener {

    private Activity mActivity;
    private int mRequestCode;
    private String mRationale;
    private View mView;
    private TextView mTvConfirm;
    private TextView mTvCancel;
    private TextView mTvmsg;

    private PermissionDialog(Activity activity, String rationale, int requestCode) {
        this(activity, R.style.AD_interstitial_dialog);
        this.mRationale = rationale;
        this.mRequestCode = requestCode;
        this.mActivity = activity;
        initView();
        initData();
        initEvent();
    }

    private PermissionDialog(Context context, int themeResId) {
        super(context, themeResId);
    }

    private void initView() {
        mView = View.inflate(mActivity, R.layout.dialog_permission, null);
        mTvConfirm = (TextView) mView.findViewById(R.id.dialog_permission_confirm);
        mTvCancel = (TextView) mView.findViewById(R.id.dialog_permission_cancel);
        mTvmsg = (TextView) mView.findViewById(R.id.dialog_permission_msg);
        int width = AppUtil.dip2px(266);
        int height = AppUtil.dip2px(191);
        setContentView(mView, new ActionBar.LayoutParams(width, height));
    }

    private void initData(){
        mTvmsg.setText(mRationale);
    }

    private void initEvent() {
        this.setCanceledOnTouchOutside(false);
        mTvConfirm.setOnClickListener(this);
        mTvCancel.setOnClickListener(this);
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.dialog_permission_confirm:
                this.dismiss();
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){ //android 6.0以上的跳转到权限管理界面
                    Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                    Uri uri = Uri.fromParts("package", mActivity.getPackageName(), null);
                    intent.setData(uri);
                    mActivity.startActivityForResult(intent,mRequestCode);
                }else{ //6.0以下的跳转到设置界面
                    Intent intent =  new Intent(Settings.ACTION_SETTINGS);
                    mActivity.startActivity(intent);
                }
                if(mRequestCode==112){ //存储权限如果没有的话关闭程序
                    mActivity.finish();
                }
                break;
            case R.id.dialog_permission_cancel:
                this.dismiss();
                if(mRequestCode==112){ //存储权限如果没有的话关闭程序
                    mActivity.finish();
                }
                break;
        }
    }

    public static class Builder {

        private final Activity mActivity;
        private final String mRationale;
        private int mRequestCode;

        public Builder(@NonNull Activity activity, @NonNull String rationale) {
            this.mActivity = activity;
            this.mRationale = rationale;
        }

        public Builder setRequestCode(int requestCode) {
            this.mRequestCode = requestCode;
            return this;
        }

        public PermissionDialog build() {
            return new PermissionDialog(mActivity, mRationale, mRequestCode);
        }
    }
}
